﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AssetManagementSystem.Utils;
using AssetManagementSystem.Model.IDao;

namespace AssetManagementSystem.Model {

    public class StudentManager {

        IStudentDAO dao;

        public StudentManager() {
            //get the student dao
            dao = DAOManager.GetStudentDAO();
        }

        //====================================================================
        //  STANDARD CRUD METHODS
        //====================================================================

        public void CreateStudent(Student newStudent) {
            dao.CreateStudent(newStudent);
        }

        public List<Student> GetAllStudent() {
            return dao.GetAllStudents().ToList<Student>();
        }

        public Student GetStudent(string id) {
            return dao.GetStudent(id);
        }

        public void UpdateStudent(Student existingStudent) {
            dao.UpdateStudent(existingStudent);
        }

        public void DeleteStudent(string id) {
            dao.DeleteStudent(id);
        }

        //====================================================================
        //  END - STANDARD CRUD METHODS
        //====================================================================

        //====================================================================
        //  ENHANCED METHODS
        //====================================================================


        /// <summary>
        /// Attempts to find students contain part, or all of the input text in their string representations (ToString()).
        /// The results are sorted by relevance.
        /// </summary>
        /// <param name="input">The text to search for</param>
        /// <returns>The list of sorted results</returns>
        public List<Student> FindStudents(string input) {
            //Get all students
            List<Student> allStudents = GetAllStudent();
            //Create a list to store results
            List<SortObject> sortList = new List<SortObject>();
            //Get all students with at least one tri-gram match to the input
            foreach (Student student in allStudents) {
                int ngramMatches = SearchUtil.NGramSearch(input, student.ToString(), 3);
                if(ngramMatches > 0){
                    sortList.Add(new SortObject(ngramMatches, student));
                }
            }
            //Sort the results by the number of matches they each had
            sortList.Sort();
            //Turn the results back into a list of students
            List<Student> matchedStudents = new List<Student>();
            foreach (SortObject obj in sortList) {
                matchedStudents.Add((Student)obj.TheObject);
            }
            //return the sorted results
            return matchedStudents;
        }

        //====================================================================
        //  END - ENHANCED METHODS
        //====================================================================

    }
}
